package com.abcdefg.rabbitmqextensionsdemo.config;

import com.abcdefg.rabbitmqextensionsdemo.constants.Constant;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DLConfig {
//    @Bean("normalQueue")
//    public Queue normalQueue(){
//        return QueueBuilder.durable(Constant.NORMAL_QUEUE)
//                .deadLetterExchange(Constant.DL_EXCHANGE)
//                .deadLetterRoutingKey("dlx")
//                .ttl(20000)
//                .maxLength(10L)
//                .build();
//    }
    //用来演示TTL+死信实现的延迟队列发送消息存在的问题
    @Bean("normalQueue")
    public Queue normalQueue(){
        return QueueBuilder.durable(Constant.NORMAL_QUEUE)
                .deadLetterExchange(Constant.DL_EXCHANGE)
                .deadLetterRoutingKey("dlx")
                .build();
    }
    @Bean("normalExchange")
    public DirectExchange normalExchange(){
        return ExchangeBuilder.directExchange(Constant.NORMAL_EXCHANGE).build();
    }
    @Bean("bindingNormal")
    public Binding bindingNormal(@Qualifier("normalQueue") Queue normalQueue,
                                 @Qualifier("normalExchange") DirectExchange normalExchange){
        return BindingBuilder.bind(normalQueue).to(normalExchange).with("normal");
    }

    @Bean("dlQueue")
    public Queue dlQueue(){
        return QueueBuilder.durable(Constant.DL_QUEUE).build();
    }
    @Bean("dlExchange")
    public DirectExchange dlExchange(){
        return ExchangeBuilder.directExchange(Constant.DL_EXCHANGE).build();
    }
    @Bean("bindingDl")
    public Binding bindingDl(@Qualifier("dlQueue") Queue dlQueue,
                                 @Qualifier("dlExchange") DirectExchange dlExchange){
        return BindingBuilder.bind(dlQueue).to(dlExchange).with("dlx");
    }
}
